import { defineStore } from "pinia";
interface Tree {
  id: string | number;
  label: string;
  children?: Tree[];
  otherData?: any;
}
class AppStore {
  treeData: Array<Tree> = [
    {
      id: "root",
      label: "表",
      children: [
        {
          label: "table_0",
          id: "node_1756396535897",
          otherData: {
            id: "node_1756396535897",
            text: "table_0",
            x: 275,
            y: 155,
            color: "#3783c0",
            data: {
              columns: [
                {
                  name: "id",
                  type: "int",
                  length: 0,
                  decimalPoint: 0,
                  isNull: "0",
                  virtual: "0",
                  key: "0",
                  annotation: "",
                  id: 1756396546754,
                },
              ],
              foreignKey: [],
            },
            nodeShape: 0,
          },
        },
        {
          label: "table_1",
          id: "node_1756396537077",
          otherData: {
            id: "node_1756396537077",
            text: "table_1",
            x: 624,
            y: 261,
            color: "#3783c0",
            data: {
              columns: [
                {
                  name: "id",
                  type: "int",
                  length: 0,
                  decimalPoint: 0,
                  isNull: "0",
                  virtual: "0",
                  key: "0",
                  annotation: "",
                  id: 1756396561837,
                },
                {
                  name: "ids",
                  type: "varchar",
                  length: 255,
                  decimalPoint: 0,
                  isNull: "0",
                  virtual: "0",
                  key: "0",
                  annotation: "",
                  id: 1756396570326,
                },
              ],
              foreignKey: [
                {
                  fatherTable: "node_1756396535897",
                  field: 1756396561837,
                  fieldsReferenced: 1756396546754,
                  id: 1757945270152,
                  name: "11",
                  pattern: "Default",
                  // whenDeleting: "",
                  // whenUpdating: "",
                },
              ],
            },
            nodeShape: 1,
          },
        },
      ],
    },
  ];
  count: number = 0;
  lines: any[] = [
    // { from: "node_1756396537077", to: "node_1756396535897" }
  ];
}

export const useApp = defineStore("app", {
  state() {
    return new AppStore();
  },
  actions: {
    add(data: any) {
      this.treeData[0].children!.push(data);
    },
    clear() {
      this.treeData[0].children = [];
    },
    editName(nodeId: string | number, value: string) {
      const treeIndex = this.treeData[0].children!.findIndex(
        (n) => n.id === nodeId
      );
      if (treeIndex > -1) {
        this.treeData[0].children![treeIndex].label = value;
      }
    },
    del(nodeId: string | number) {
      const delIndex = this.treeData[0].children!.findIndex(
        (n) => n.id == nodeId
      );
      if (delIndex > -1) {
        this.treeData[0].children!.splice(delIndex, 1);
      }
    },
    setOtherData(id: string | number, otherData: any) {
      const treeIndex = this.treeData[0].children!.findIndex(
        (n) => n.id === id
      );
      if (treeIndex > -1) {
        this.treeData[0].children![treeIndex].otherData.data = {
          columns: otherData.field,
          foreignKey: otherData.foreignKey,
        };
      }
    },
    setCount() {
      this.count++;
    },
    setLines(data: any) {
      this.lines = data;
    },
    setNodeShape(id: string) {
      const _run = () => {
        this.treeData[0].children = this.treeData[0].children!.map((it) => {
          return {
            ...it,
            otherData: {
              ...it.otherData,
              nodeShape: 0,
            },
          };
        });
      };
      _run();
      const treeIndex = this.treeData[0].children!.findIndex(
        (n) => n.id === id
      );

      if (treeIndex > -1) {
        this.treeData[0].children![treeIndex].otherData.nodeShape = 1;
      } else {
        _run();
      }
    },
  },
  // persist: {
  //   storage: localStorage,
  // },
});
